//
//  SystemMessageTableViewCell.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/28.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "SystemMessageTableViewCell.h"

#define FONT_COLOR RGB_COLOR(133, 133, 133, 133)

@interface SystemMessageTableViewCell ()

@property (nonatomic, strong) UIView * grayLineView; // 灰色下边线
@property (nonatomic, strong) NSMutableDictionary * paramters; // POST请求参数

@end

@implementation SystemMessageTableViewCell

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 去掉选中状态
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 添加视图
        [self addSubview:self.nameLabel];
        [self addSubview:self.dateLabel];
        [self addSubview:self.grayLineView];
        [self addSubview:self.agreeButton];
        [self addSubview:self.refuseButton];
        [self addSubview:self.lookFriendInfoButton];
        [self addSubview:self.deleteButton];
        
        // 自动布局
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(SCREEN_WIDTH * 0.05);
            make.centerY.equalTo(self).multipliedBy(0.62);
        }];
        [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLabel);
            make.centerY.equalTo(self).multipliedBy(1.42);
        }];
        [_grayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self);
            make.height.offset(2);
            make.centerX.equalTo(self);
            make.bottom.offset(0);
        }];
        [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(self).multipliedBy(0.26);
            make.width.equalTo(_agreeButton.mas_height).multipliedBy(3.3);
            make.centerX.equalTo(self).multipliedBy(1.2);
            make.centerY.equalTo(self).multipliedBy(1.55);
        }];
        [_refuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(_agreeButton);
            make.left.equalTo(_agreeButton.mas_right).offset(SCREEN_WIDTH * 0.02);
            make.centerY.equalTo(_agreeButton);
        }];
        [_lookFriendInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(_agreeButton);
            make.width.equalTo(_lookFriendInfoButton.mas_height).multipliedBy(4.1);
            make.left.equalTo(_refuseButton.mas_right).offset(SCREEN_WIDTH * 0.02);
            make.centerY.equalTo(_agreeButton);
        }];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(_agreeButton);
            make.centerY.equalTo(_agreeButton);
            make.right.equalTo(_lookFriendInfoButton);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToAgreeButton {
    [self.paramters setObject:@"Agree" forKey:@"ref"];
    
    [self agreeOrRefuse];
}
- (void)respondsToRefuseButton {
    [self.paramters setObject:@"disAgree" forKey:@"ref"];
    
    [self agreeOrRefuse];
}
- (void)respondsToLookFriendInfoButton {
    _reloadDataBlock(_friendid);
}
- (void)respondsToDeleteButton {
    NSMutableDictionary * paramters = [NSMutableDictionary dictionary];
    [paramters setObject:_friendid forKey:@"mid"];
    [paramters setObject:[LoginUserInfo defaultUserInfo].userid forKey:@"uid"];
    
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"删除中..." url:BASEURLAPPEND(PERSONALHOMEPAGE_SYSTEMMESSAGE_DELETE_URL) parameters:paramters finish:^(id responseObject, NSError *error) {
        if (!error) {
            if ([responseObject[@"status"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"删除成功！"];
                
                // 刷新表格视图
                _reloadDataBlock(NULL);
            } else {
                [MBProgressHUD showSuccess:@"删除失败！"];
            }
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
    }];
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)agreeOrRefuse {
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"处理中..." url:BASEURLAPPEND(PERSONALHOMEPAGE_SYSTEMMESSAGE_ALLOWORNOT_URL) parameters:_paramters finish:^(id responseObject, NSError *error) {
        NSLog(@"%@", responseObject);
        if (!error) {
            if ([responseObject[@"status"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"处理完成"];
                
                // 刷新表格视图
                _reloadDataBlock(NULL);
            } else {
                [MBProgressHUD showSuccess:responseObject[@"data"]];
            }
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
    }];
}
- (void)reloadData:(ReloadDataBlock)reloadDataBlock {
    _reloadDataBlock = reloadDataBlock;
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = FONT_COLOR;
    }
    return _nameLabel;
}
- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.textColor = FONT_COLOR;
    }
    return _dateLabel;
}
- (UIView *)grayLineView {
    if (!_grayLineView) {
        _grayLineView = [[UIView alloc] init];
        _grayLineView.backgroundColor = RGB_COLOR(124, 123, 123, 1);
    }
    return _grayLineView;
}
- (UIButton *)agreeButton {
    if (!_agreeButton) {
        _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeButton setImage:[UIImage imageNamed:@"i_up_同意_03.png"] forState:UIControlStateNormal];
        [_agreeButton addTarget:self action:@selector(respondsToAgreeButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeButton;
}
- (UIButton *)refuseButton {
    if (!_refuseButton) {
        _refuseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_refuseButton setImage:[UIImage imageNamed:@"i_up_拒绝_03.png"] forState:UIControlStateNormal];
        [_refuseButton addTarget:self action:@selector(respondsToRefuseButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refuseButton;
}
- (UIButton *)lookFriendInfoButton {
    if (!_lookFriendInfoButton) {
        _lookFriendInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lookFriendInfoButton setImage:[UIImage imageNamed:@"i_up_查看好友资料_03.png"] forState:UIControlStateNormal];
        [_lookFriendInfoButton addTarget:self action:@selector(respondsToLookFriendInfoButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lookFriendInfoButton;
}
- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.hidden = YES;
        [_deleteButton setImage:[UIImage imageNamed:@"i_up_Delete.png"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(respondsToDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}
- (NSMutableDictionary *)paramters {
    if (!_paramters) {
        _paramters = [NSMutableDictionary dictionary];
        [_paramters setObject:_friendid forKey:@"fid"];
        [_paramters setObject:[LoginUserInfo defaultUserInfo].userid forKey:@"uid"];
    }
    return _paramters;
}

@end
